<%-- 
    Document   : editStudent
    Created on : Dec 18, 2014, 3:23:04 PM
    Author     : nga
--%>
<jsp:include page="../header.jsp"></jsp:include>
<%@page import="entity.Student"%>
<%@page import="dao.StudentDAO"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.Connection"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!doctype html> 
<%
    String msg = "";
    String id = request.getParameter("id");
    Student stu = StudentDAO.getStudent(request.getParameter("id"));
    String saveBtn = request.getParameter("saveBtn");
    String name;
    String xage;
    String address;
    String gender = "M";
    String batch = "";
    String image = "";
    if (saveBtn == null) {
        name = stu.getName();
        xage = String.valueOf(stu.getAge());
        address = stu.getAddress();
        gender = String.valueOf(stu.getGender());
        batch = stu.getBatch();
        image = stu.getImage();
    } else {
        name = request.getParameter("name");
        xage = request.getParameter("age");
        address = request.getParameter("address");
        gender = request.getParameter("gender");
        batch = request.getParameter("batch");
        image = request.getParameter("image");
    }
    
    int age = 0;
    try {
        age = Integer.parseInt(xage);
    } catch (Exception e) {}
    
    Student newStu = new Student(id, name, age, address, gender.charAt(0), batch, image);
    if (saveBtn != null) {
        StudentDAO.edit(newStu);
        response.sendRedirect("listStudent.jsp");
    }
%>
<!doctype html>
<html>
    <head>
        <meta charset="utf-8">
        <title>Untitled Document</title>
    </head>

    <body>
        <div class="container">
            <h2>Edit Student</h2>
            <hr>
            <form action="editStudent.jsp?id=<%=id%>" method="POST">
                <table width="50%" style="border-collapse: separate; border-spacing: 20px">
                    <tr>
                        <td width="30%">&nbsp;</td>
                        <td width="70%">&nbsp;</td>
                    </tr>

                    <tr>
                        <td>Student ID(<font color="red">*</font>) </td>
                        <td><input name="id" type="text" class="form-control" value="<%=id%>" disabled=""/></td>
                    </tr>
                    <tr>
                        <td>Name(<font color="red">*</font>) </td>
                        <td><input name="name" type="text" class="form-control" value="<%=name%>" required/></td>
                    </tr>
                    <tr>
                        <td>Age(<font color="red">*</font>) </td>
                        <td><input name="age"  type="number" class="form-control" value="<%=age%>" required/></td>                      
                    </tr>
                    <tr>
                        <td>Address(<font color="red">*</font>) </td>
                        <td><input name="address" type="text"  class="form-control" value="<%=address%>" required/></td>
                    </tr>
                    <tr>
                        <td>Gender(<font color="red">*</font>) </td>
                        <td>
                            <input type="radio" name="gender" value="M" <%if (gender != null && gender.equals("M")) {%> checked="" <%}%>/> Male
                            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                            <input type="radio" name="gender" value="F" <%if (gender != null && gender.equals("F")) {%> checked="" <%}%>/> Female
                        </td>
                    </tr>
                    <tr>
                        <td>Batch(<font color="red">*</font>) </td>
                        <td><input name="batch" type="text" class="form-control" value="<%=batch%>" required/></td>
                    </tr>
                    <tr>
                        <td>Image </td>
                        <td><input name="image" type="file" value="<%=image%>"/></td>
                    </tr>
                    <tr>
                        <td>&nbsp;</td>
                        <td>&nbsp;</td>
                    </tr>
                    <tr>
                        <td><input name="saveBtn" type="submit" value="Save" class="btn btn-success" style="width: 100px"/></td>
                        <td><a href="listStudent.jsp"><input type="button" value="Cancel" class="btn btn-danger" style="width: 100px"/></a></td>
                    </tr>
                </table>
            </form>
        </div>
    </body>
</html>
<jsp:include page="../footer.jsp"></jsp:include>